<template>
  <div class="notification">
    <div class="header-container clearfix">
      <el-button
        type="primary"
        icon="el-icon-reading"
        size="small"
        @click="setAllNotificationsAsRead"
      >设置所有为已读
      </el-button>
      <el-button
        type="success"
        icon="el-icon-setting"
        size="small"
        @click="$refs.notificationSetting.open()"
      >通知设置
      </el-button>
    </div>
    <paged-table
      ref="nftable"
      :tb-height="tbHeight"
      :entity-name="entityName"
      :request-param="pagerequest"
    >
      <template slot="FilterItems">
        <el-select
          v-model="pagerequest.state"
          size="small"
          class="filter-item "
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </template>

      <template slot="TableColumns">
        <el-table-column
          :label="L('Actions')"
          width="100"
        >
          <template slot-scope="{row}">
            <el-link
              type="primary"
              style="margin-right:10px"
              @click="setNotificationAsRead(row.id)"
            >
              <svg-icon :icon-class="isReadIcon(row.state)" />
            </el-link>
            <el-link
              type="danger"
              icon="el-icon-delete"
              @click="deletNotification(row.id)"
            />
          </template>
        </el-table-column>

        <el-table-column
          prop="message"
          label="通知"
        >
          <template slot-scope="{row}">
            <i :class="row.notification.severity | notificationSeverityIcon" /> <span> {{ row.notification.data | formatMessage }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="creationTime"
          :label="L('CreationTime')"
        >
          <template slot-scope="{row}">
            <span>{{ row.notification.creationTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
      </template>

    </paged-table>

    <notification-setting ref="notificationSetting" />
  </div>
</template>

<script>
import pagedTable from '@/components/PagedTable'
import NotificationSetting from '@/layout/components/Navbar/NotificationSetting'

export default {
  name: 'Notification',
  components: { pagedTable, NotificationSetting },
  data() {
    return {
      entityName: 'notification',
      pagerequest: { state: null },
      options: [{
        value: null,
        label: '全部'
      }, {
        value: 0,
        label: '未读'
      }, {
        value: 1,
        label: '已读'
      }]
    }
  },
  computed: {
    tbHeight() {
      return document.body.clientHeight - 217
    }
  },
  methods: {
    isReadIcon(state) {
      return state ? 'read' : 'unread'
    },

    async setNotificationAsRead(id) {
      await this.$store.dispatch('notification/setNotificationAsRead', id)
      this.$refs.nftable.getpage()
    },

    async setAllNotificationsAsRead() {
      await this.$store.dispatch('notification/setAllNotificationsAsRead')
      this.$refs.nftable.getpage()
    },

    deletNotification(id) {
      this.$confirm(`此通知将被删除`, '你确定吗删除？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await this.$store.dispatch('notification/deleteNotification', id)
        this.$refs.nftable.getpage()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss">
.notification {
  .header-container {
    .el-button {
      float: right;
      margin-top: 10px;
      margin-right: 20px;
    }
  }
}
</style>

